package xyz.labmem.base.sys.module.menu.service.impl

import cn.hutool.core.util.ClassUtil
import com.baomidou.mybatisplus.annotation.TableName
import io.swagger.annotations.Api
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.RequestMapping
import xyz.labmem.base.core.annotion.SocketPath
import xyz.labmem.base.sys.consts.PathPrefixConst
import xyz.labmem.base.sys.module.menu.entity.SysUserMenus
import xyz.labmem.base.sys.module.menu.entity.response.MenusAllRes
import xyz.labmem.base.sys.module.menu.entity.response.MenusRes
import xyz.labmem.base.sys.module.menu.mapper.SysUserMenusMapper
import xyz.labmem.base.sys.module.menu.service.MenuService
import xyz.labmem.base.sys.module.user.entity.SysUser

@Service
class MenuServiceImpl(
    private var sysUserMenusMapper: SysUserMenusMapper
)  : MenuService {
    override fun getAmountByModel(pack: String): ArrayList<MenusRes> {
        val res = ArrayList<MenusRes>()
        var classs = ClassUtil.scanPackage("${pack}.entity")
        classs.forEach {
            //获取controller
            if("${pack}.entity" == it.packageName){
                val contr = Class.forName("${pack}.controller.${it.simpleName}_Controller")
                val api = contr.getAnnotation(Api::class.java)
                val req = contr.getAnnotation(RequestMapping::class.java)
                val open = it.getAnnotation(SocketPath::class.java)

                res.add(MenusRes().apply {
                    name = api.tags[0]
                    prefix = req.value[0].replace(PathPrefixConst.APP_PATH_PREFIX, "")
                    index = open?.order?:1
                })
            }
        }
        res.sortBy { user -> user.index }
        return res
    }

    override fun getAmountByUser(pack: String, user : SysUser): Boolean {
        if(user.superr==1){
            return true
        }
        var menus = sysUserMenusMapper.selectListAll(user.id)
        var classs = ClassUtil.scanPackage("${pack}.entity")
        classs.forEach {
            //获取controller
            if("${pack}.entity" == it.packageName){
                val contr = Class.forName("${pack}.controller.${it.simpleName}_Controller")
                val req = contr.getAnnotation(RequestMapping::class.java)
                var prefix = req.value[0].replace(PathPrefixConst.APP_PATH_PREFIX, "")
                if(menus.contains(SysUserMenus().apply { userId=user.id;menuId=prefix })){
                    return true
                }
            }
        }
        return false
    }

    override fun getMoudlByUser(pack: String, user : SysUser): ArrayList<MenusAllRes> {
        var menus = sysUserMenusMapper.selectListAll(user.id)

        val res = ArrayList<MenusAllRes>()
        var classs = ClassUtil.scanPackage("${pack}.entity")
        classs.forEach {
            //获取controller
            if("${pack}.entity" == it.packageName){
                val contr = Class.forName("${pack}.controller.${it.simpleName}_Controller")
                val api = contr.getAnnotation(Api::class.java)
                val req = contr.getAnnotation(RequestMapping::class.java)
                val open = it.getAnnotation(SocketPath::class.java)
                var pre = req.value[0].replace(PathPrefixConst.APP_PATH_PREFIX, "")
                if(user.superr==1 || menus.contains(SysUserMenus().apply { userId=user.id;menuId=pre })){
                    res.add(MenusAllRes().apply {
                        name = api.tags[0]
                        prefix = pre
                        openType = open?.value == "true"
                        issub = open?.issub == true
                        index = open?.order?:1
                    })
                }
            }
        }
        res.sortBy { user -> user.index }
        return res
    }
}